/**
 * Copyright (c) 2008-2011 FeiLong, Inc. All Rights Reserved.
 * <p/>
 * This software is the confidential and proprietary information of FeiLong
 * Network Technology, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with FeiLong.
 * <p/>
 * FeiLong MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. FeiLong SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 * <p/>
 */
package com.feilong.spring.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * spring 工具类
 * 
 * @author <a href="mailto:venusdrogon@163.com">金鑫</a>
 * @version 1.0 2011-3-31 下午06:08:20
 */
public class SpringUtil{

	@SuppressWarnings("unused")
	private static final Logger	log	= LoggerFactory.getLogger(SpringUtil.class);

	/**
	 * 普通类获得spring 注入的类方法(如果找不到bean会抛出异常)
	 * 
	 * @param beanName
	 *            xml文件中配置的bean beanName
	 * @param request
	 *            request
	 * @return 注入的bean
	 */
	public static Object getRequiredBean(String beanName,HttpServletRequest request){
		ServletContext servletContext = request.getSession().getServletContext();
		// getRequiredWebApplicationContext 如果是空会抛出异常
		ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		return applicationContext.getBean(beanName);
	}

	/**
	 * 普通类获得spring 注入的类方法(如果找不到bean,返回null)
	 * 
	 * @param beanName
	 *            xml文件中配置的bean beanName
	 * @param request
	 *            request
	 * @return 注入的bean
	 */
	public static Object getBean(String beanName,HttpServletRequest request){
		ServletContext servletContext = request.getSession().getServletContext();
		// getWebApplicationContext 如果是空,返回null
		ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		return applicationContext.getBean(beanName);
	}
}
